
import Taro from "@tarojs/taro";
import { getTokenLocal } from "@/untils";
const baseUrl = process.env.TARO_APP_BASEURL

type RequestParamsType = {
    url: string,
    method: 'GET' | 'POST',
    data?: any,
    header?: any,
    timeout?: any,
    loadingtitle?: string,
    [key: string]: any
}

export const request = (params: RequestParamsType) => {
    const { url, method, data, header, timeout = 6000,loadingtitle = '' } = params
    Taro.showLoading({
        title: loadingtitle,
        mask: true
    })
    return new Promise((resolve) => {
        Taro.request({
            data,
            method,
            timeout,
            url: url,
            header: {
                'content-type': 'application/json;charset=UTF-8,text/plain,*/*',
                token:getTokenLocal(),
                ...header
            },
            success(res) {
                Taro.hideLoading()
                if (res.data.code === 200) { // 具体参考接口响应的数据结构定义
                    if (Array.isArray(res.data.data)) {
                        resolve(res.data.data)
                    } else {
                        resolve({ ...res.data.data, success: true })
                    }
                } else {
                    resolve({ message: res.data.data.message, success: false })
                    showError(res.data.data.message)
                }
            },
            fail(res) {
                Taro.hideLoading()
                console.log('fail', res)
                resolve({ message: res, success: false })
                showError('请求失败' )
            },
        }).catch((e) => {
            Taro.hideLoading()
            console.log('catch err', e)
            resolve({ message: e.errMsg, success: false })
            showError(e.errMsg )
        })
    })
}
type RequestParamsFilesType = {
    url: string,
    tempFilePaths:string,
    header?: any,
    timeout?: any,
    loadingtitle?: string,
    cb?:(progress:any)=>void
    [key: string]: any
}

export const requestFile = (params: RequestParamsFilesType) => {
    const { url, tempFilePaths, header, timeout = 60000,loadingtitle = '',cb } = params

    return new Promise((resolve) => {
        Taro.uploadFile({
            name: 'file',
            filePath:tempFilePaths,
            timeout,
            url: url,
            header: {   
                token:getTokenLocal(),
                ...header
            },
            success(res) {
                const data = JSON.parse(res.data)
                if (data.code === 200) { // 具体参考接口响应的数据结构定义
                    resolve({ ...data, success: true })
                } else {
                    resolve({ ...data, success: false })
                    showError('请求失败了')
                }
            },
            fail(res) {
                resolve({ message: res, success: false })
                showError('请求失败' )
            },
        }).onProgressUpdate((progress)=>{
            if(cb){
                cb(progress)
              }
        })
    })
}


function showError(message: string, duration = 1500) {
    Taro.showToast({
        title: message,
        icon: 'none', // 'error' 'success' 'loading' 'none'
        duration: duration
    })
}
